package cn.pet.service.manage.controller;


import cn.pet.service.manage.auth.JWTUtil;
import cn.pet.service.manage.common.Response;
import cn.pet.service.manage.entity.PetSubscribe;
import cn.pet.service.manage.entity.PetSubscribeOrder;
import cn.pet.service.manage.entity.api.Result;
import cn.pet.service.manage.entity.dto.SubscribeDTO;
import cn.pet.service.manage.service.PetSubscribeOrderService;
import cn.pet.service.manage.service.PetSubscribeService;
import cn.pet.service.manage.utils.OrderIDGenerate;
import cn.pet.service.manage.utils.SecurityUtil;
import cn.pet.service.manage.utils.uuid.IdUtils;
import cn.pet.service.manage.utils.uuid.UUID;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 预约服务信息 前端控制器
 * </p>
 *
 * @author
 * @since 2023-02-11
 */
@RestController
@RequestMapping("/manage/petSubscribe")
public class PetSubscribeController {

    @Resource
    private PetSubscribeService petSubscribeService;

    @Resource
    private PetSubscribeOrderService petSubscribeOrderService;


    /**
     * 查看预约列表
     * @param subscribeDTO
     * @return
     */
    @GetMapping("list")
    public Object list(SubscribeDTO subscribeDTO){
        return Result.ok("success",new PageInfo<>(petSubscribeService.listByPage(subscribeDTO)));
    }


    @PostMapping("save")
    public Object save(@RequestBody PetSubscribe petSubscribe){
        Integer userId = SecurityUtil.getUserId();
        if (null == userId){
            return Result.error("请先登录");
        }
        petSubscribe.setUserId(userId);
        if (!petSubscribeService.save(petSubscribe)){
            return Result.error("预约失败");
        }
        return Result.ok("预约成功");
    }

    /**
     * 审批预约列表
     * 1:审批通过 2：审批失败
     * @param subscribeDTO
     * @return
     */
    @PutMapping ("approve")
    public Object approve(@RequestBody SubscribeDTO subscribeDTO){
        PetSubscribe petSubscribe = new PetSubscribe();
        petSubscribe.setId(subscribeDTO.getId());
        petSubscribe.setApproveUserId(SecurityUtil.getUserId());
        petSubscribe.setApproveDesc(subscribeDTO.getApproveDesc());
        petSubscribe.setStatus(subscribeDTO.getStatus());
        if (!petSubscribeService.updateById(petSubscribe)){
            return Result.error("审批失败");
        }
        return Result.ok(subscribeDTO.getStatus() == 1 ? "审批通过" : "审批驳回");
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Object detail(@PathVariable("id") Integer id){
        return Result.ok("success",petSubscribeService.getDetailById(id));
    }

    /**
     * 生成预约服务订单
     * @param petSubscribeOrder
     * @return
     */
    @PostMapping("createOrder")
    @Transactional(rollbackFor = Exception.class)
    public Object createOrder(@RequestBody PetSubscribeOrder petSubscribeOrder){
        //生成订单号
        petSubscribeOrder.setId(OrderIDGenerate.getOrderNumber());
        petSubscribeOrder.setCreateId(SecurityUtil.getUserId());
        if (!petSubscribeOrderService.save(petSubscribeOrder)){
            return Result.error("订单生成失败");
        }
        //更新状态
        PetSubscribe petSubscribe = new PetSubscribe();
        petSubscribe.setId(Integer.parseInt(petSubscribeOrder.getSubscribeId()));
        petSubscribe.setStatus(petSubscribeOrder.getStatus());
        petSubscribeService.updateById(petSubscribe);
        return Result.ok("订单生成成功");
    }


    @GetMapping("listByUserId")
    public Object listByUserId(SubscribeDTO subscribeDTO){
        Integer userId = SecurityUtil.getUserId();
        if (null == userId){
            return Result.error("请先登录");
        }
        subscribeDTO.setUserId(userId);
        return Result.ok("success",petSubscribeService.listByPage(subscribeDTO));
    }
}
